package com.duowei.diandan.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.duowei.diandan.R;
import com.duowei.diandan.bean.food.MealChildGroup;
import com.duowei.diandan.bean.food.MealChildGroupAndMealChildChecked;
import com.duowei.diandan.constant.Settings;

import java.util.List;

public class MealVAdapter extends RecyclerView.Adapter<MealVAdapter.ViewHolder> {

    private List<MealChildGroupAndMealChildChecked> mList;

    public MealVAdapter(List<MealChildGroupAndMealChildChecked> list) {
        mList = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.meal_row, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        MealChildGroupAndMealChildChecked mcgacc = mList.get(holder.getAdapterPosition());
        MealChildGroup group = mcgacc.getMealChildGroup();

        if (holder.getAdapterPosition() == 0) {
            holder.mLineView.setVisibility(View.GONE);
        } else {
            holder.mLineView.setVisibility(View.VISIBLE);
        }
        holder.mCateView.setText(group.getCateStr());

        holder.mRecyclerView.setAdapter(new MealHAdapter(group.getChildList(), mcgacc.getMealChildChecked()));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        View mLineView;
        TextView mCateView;
        RecyclerView mRecyclerView;

        public ViewHolder(View itemView) {
            super(itemView);
            mLineView = itemView.findViewById(R.id.lineView);
            mCateView = itemView.findViewById(R.id.cateView);
            mRecyclerView = itemView.findViewById(R.id.recyclerView);
            GridLayoutManager layoutManager = new GridLayoutManager(itemView.getContext(),
                    Settings.COLUMN_MEAL, GridLayoutManager.VERTICAL, false);
            mRecyclerView.setLayoutManager(layoutManager);
            mRecyclerView.setHasFixedSize(true);
        }
    }
}
